/*
 * @Author: hzm 
 * @Date: 2020-06-22 17:07:40 
 * @Last Modified by: hzm
 * @Last Modified time: 2020-06-22 20:07:39
 */

#pragma once
#include <thread>
#include <atomic>
#include <vector>
#include "EventLoop.h"


namespace HeyIM {

class LoopThreadPool {

public:

    LoopThreadPool(int threadNum);

    LoopThreadPool(const LoopThreadPool&) = delete;
    LoopThreadPool& operator=(const LoopThreadPool&) = delete;

    ~LoopThreadPool();

    int start();

    void stop();

    EventLoopPtr nextLoop();
    EventLoopPtr loop(event_base* base);

protected:

    enum class Status {
        None,
        Running,
        Stopping,
        Stopped
    };

    std::atomic<Status> m_status;
    std::vector<EventLoopPtr> m_loops;
    std::vector<std::thread> m_threads;
    int m_threadNum = 1;
    std::atomic<int> m_threadStopNum;
    std::atomic<int> m_threadRunNum;
    int m_nextLoopID = 0;

};

}

